package com.dhcc.bpm.alarm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.alarm.entity.AlarmObjGroup;
import com.dhcc.bpm.alarm.entity.AlarmObjectGroup;
import com.dhcc.bpm.alarm.mapper.AlarmObjGroupMapper;
import com.dhcc.bpm.alarm.service.IAlarmObjGroupService;
import com.dhcc.bpm.alarm.service.IAlarmObjectGroupService;
import com.dhcc.bpm.alarm.vo.AlarmObjGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiafayu
 * @since 2022-02-16
 */
@Slf4j
@Service
public class AlarmObjGroupServiceImpl extends ServiceImpl<AlarmObjGroupMapper, AlarmObjGroup> implements IAlarmObjGroupService {
    @Autowired
    AlarmObjGroupMapper alarmObjGroupMapper;

    @Autowired
    IAlarmObjectGroupService alarmObjectGroupService;
    
    
    @Override
    public IPage<AlarmObjGroupVO> selectAlarmObjGroupPage(IPage<AlarmObjGroupVO> page, AlarmObjGroupVO AlarmObjGroup) {
        return page.setRecords(baseMapper.selectAlarmObjGroupPage(page, AlarmObjGroup));
    }

    @Transactional
	@Override
	public boolean saveOrUpdate(AlarmObjGroupVO alarmObjGroupVo) {
    	
    	// 是否新增
    	boolean isInsert = (null == alarmObjGroupVo.getId()) ? true : false;  
		
    	// 插入/或修改分组表数据
    	super.saveOrUpdate(alarmObjGroupVo); 
    	
    	// 要插入的关联关系集合 
    	List<AlarmObjectGroup> objectGroupList = new ArrayList<AlarmObjectGroup>();
		
		// 插入/修改关联关系表
		if(isInsert) { 
			List<String> objectNoList = Arrays.asList(alarmObjGroupVo.getObjectno().split(","));
			for(String objectNo : objectNoList) {
				objectGroupList.add(new AlarmObjectGroup(Long.parseLong(objectNo), alarmObjGroupVo.getId()));
			}
			
		}else { // 修改 
			
			// 删除已经取消勾选的关联关系 
			if(StringUtils.isNotBlank(alarmObjGroupVo.getObjectnoDelete())) {
				String[] objectNoAry = alarmObjGroupVo.getObjectnoDelete().split(",");
				
				// 字符串数组 转 List<Long>
		        List<Long> objectIds = Arrays.stream(objectNoAry).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
		        
				alarmObjectGroupService.removeByObjectIds(objectIds, alarmObjGroupVo.getId());
			}
			
			// 新增关联关系 
			if(StringUtils.isNotBlank(alarmObjGroupVo.getObjectnoAdd())) {
				List<String> objectNoList = Arrays.asList(alarmObjGroupVo.getObjectnoAdd().split(","));
				for(String objectNo : objectNoList) {
					objectGroupList.add(new AlarmObjectGroup(Long.parseLong(objectNo), alarmObjGroupVo.getId()));
				}
			}
		}
		
		// 插入新关联关系
    	if(objectGroupList.size() > 0) {
    		alarmObjectGroupService.saveBatch(objectGroupList);
    	}
		
		return true;
	}

	@Override
	public boolean removeByIds(List<Long> ids) {
		log.info("删除告警对象组:{}", ids);
		
		alarmObjectGroupService.removeByGroupIds(ids);
		
		return super.removeByIds(ids);
	}
}
